 package com.wsjc.common.redis.agent;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.wsjc.common.core.config.JedisConfig;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Component
public class JedisAgent {

	private static List<JedisPool> servers = new ArrayList<JedisPool>();
	
	
	private static JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
	
	static {
		
//		<!-- 最大连接数 -->
		jedisPoolConfig.setMaxTotal(Integer.valueOf(JedisConfig.readConfig("jedis.config.maxTotal")));
//		<!-- 最大空闲连接数 -->
		jedisPoolConfig.setMaxIdle(Integer.valueOf(JedisConfig.readConfig("jedis.config.maxIdle")));
//		<!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
		jedisPoolConfig.setMaxWaitMillis(Integer.valueOf(JedisConfig.readConfig("jedis.config.maxWaitMillis")));
//		<!-- 连接最小空闲时间 -->
		jedisPoolConfig.setMinEvictableIdleTimeMillis(Integer.valueOf(JedisConfig.readConfig("jedis.config.minEvictableIdleTimeMillis")));
//		<!-- 每次释放连接的最大数目 -->
		jedisPoolConfig.setNumTestsPerEvictionRun(Integer.valueOf(JedisConfig.readConfig("jedis.config.numTestsPerEvictionRun")));
//		<!-- 释放连接的扫描间隔（毫秒） -->
		jedisPoolConfig.setTimeBetweenEvictionRunsMillis(Long.valueOf(JedisConfig.readConfig("jedis.config.numTestsPerEvictionRun")));
//		<!-- 连接最小空闲时间 --> -->
		jedisPoolConfig.setMinEvictableIdleTimeMillis(Long.valueOf(JedisConfig.readConfig("jedis.config.timeBetweenEvictionRunsMillis")));
//		<!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
		jedisPoolConfig.setSoftMinEvictableIdleTimeMillis(Long.valueOf(JedisConfig.readConfig("jedis.config.softMinEvictableIdleTimeMillis")));
//		<!-- 在获取连接的时候检查有效性, 默认false -->
		jedisPoolConfig.setTestOnReturn(Boolean.valueOf(JedisConfig.readConfig("jedis.config.testOnBorrow")));
//		<!-- 在空闲时检查有效性, 默认false -->
		jedisPoolConfig.setTestWhileIdle(Boolean.valueOf(JedisConfig.readConfig("jedis.config.testWhileIdle")));
//		<!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
		jedisPoolConfig.setBlockWhenExhausted(Boolean.valueOf(JedisConfig.readConfig("jedis.config.blockWhenExhausted")));


	}
	
	public JedisAgent() {
		//"127.0.0.1:6379;127.0.0.1:6380;127.0.0.1:6381" ;
		String jedisAddress = JedisConfig.readConfig("jedis.agent.address") ; 
		String[] urls = jedisAddress.split(";") ;
		for(String url : urls){
	        String[] serverInfo = url.split(":");
	        String host = serverInfo[0] ;
	        int port = Integer.parseInt(serverInfo[1]) ;
	        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, 100000, JedisConfig.readConfig("redis.auth.password"));
			servers.add(jedisPool) ;
		}
	}

	// 最简单的代理实现负载均衡
	public Jedis newJedisInstance(Object key){
		
		int defmod = 1 ;
        // 获取key的长度
        int keyLenth = String.valueOf(key).length();
        // 根据key长度取模
        int mod = keyLenth % servers.size();
        
        if(mod > servers.size()){
        	// 取默认地址
        	return servers.get(defmod).getResource();
        }
        // 根据取模结果获取地址
        return servers.get(mod).getResource();
        
                    
    }

}
